package com.wondertek.liveroom;

import net.tsz.afinal.FinalBitmap;

import org.json.JSONException;
import org.json.JSONObject;

import com.jasontools.data.LiveChannelPc;
import com.jasontools.http.HttpAjaxUrl;
import com.jasontools.http.HttpManager;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PageItemViewLiveIndexTop extends RelativeLayout {
	private Context context = null;
	private ViewHolder holder = new ViewHolder();
	private LiveChannelPc objItem = null;
	
	public PageItemViewLiveIndexTop(Context context) {
		super(context);
		this.context = context;
		setupView();
	}
	
	public PageItemViewLiveIndexTop(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		setupView();
	}
	
	public PageItemViewLiveIndexTop(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		setupView();
	}
	
	private void setupView() {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.item_page_view_live_index_top, null);
		holder.ivIcon = (ImageView) view.findViewById(R.id.iv_item_page_view_live_index_top);
		holder.ivAvater = (ImageView) view.findViewById(R.id.iv_item_page_view_live_index_top_avatar);
		holder.tvTitle = (TextView) view.findViewById(R.id.tv_item_page_view_live_index_top_title);
		holder.tvOwnerName = (TextView) view.findViewById(R.id.tv_item_page_view_live_index_top_owner);
		holder.tvLooker = (TextView) view.findViewById(R.id.tv_item_page_view_live_index_top_looker);
		addView(view);
	}
	
	public void setItemData(LiveChannelPc obj) {
		this.objItem = obj;
		holder.tvTitle.setText(obj.getTitle());
		holder.tvOwnerName.setText(obj.getOwner());
		holder.tvLooker.setText(String.valueOf(obj.getPerson()));
		HttpManager.getFB(context).display(holder.ivIcon, HttpAjaxUrl.getServerUrl(HttpAjaxUrl.URL_LIVE_BG));
		HttpManager.getFB(context).display(holder.ivAvater, HttpAjaxUrl.getUserAvaterUrl(obj.getOwnerId()));
	}
	
	public void recycle() {
		holder.ivAvater.setImageBitmap(null);
		holder.ivIcon.setImageBitmap(null);
	}
	
	public void reload() {
		setItemData(this.objItem);
	}
	
	class ViewHolder {
		ImageView ivIcon;
		ImageView ivAvater;
		TextView tvTitle;
		TextView tvOwnerName;
		TextView tvLooker;
	}
}
